<?php

class ServiceAction extends Action{
	public function index(){
		import('ORG.Util.EditPage');
		$count = M('Service')->group('user_id')->select();
		$Page = new Page(count($count),10);
		$Page->setConfig('theme',"<ul class='pagination'><li><a>%totalRow% %header% %nowPage%/%totalPage% 页</a></li><li>%upPage%</li><li>%first%</li><li>%prePage%</li><li>%linkPage%</li><li>%nextPage%</li><li>%end%</li><li>%downPage%</li>".$gopagenu."</ul>");
		//$users = M('Service')->group('user_id')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->page = $Page->show();
		$users = M()->Table('box_service a')->join('box_user b on a.user_id=b.id')->field('a.*,b.name,mobile,email')->group('a.user_id')->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach ($users as $key => $value) {
			$lastContent = M('Service')->where("user_id={$value['user_id']}")->order('id desc')->field('service_id,content')->find();
			$users[$key]['reply']=$lastContent['service_id'];
			$users[$key]['content']=$lastContent['content'];
		}
		foreach ($users as $key => $value) {
			$reply[$key]=$value['reply'];
		}
		array_multisort($reply, $users); 

		$this->list = $users;
		$this->currentpage = $Page->firstRow + 1; 
		//print_r($this->list);
		$this->display();
	}

	public function getcontents(){
		$condition['user_id']=I('userid');
		$list = M('Service')->where($condition)->order("id desc")->limit(100)->select();
		foreach ($list as $key => $value) {
			$id[$key]=$value['id'];
			$list[$key]['addtime']=date('Y-m-d H:i:s',$value['addtime']);
		}
		array_multisort($id, $list); 
		$this->ajaxReturn($list,"JSON");
	}

	public function postcontents(){
		$data['user_id']=I('userid');
		$data['service_id']=1;
		$data['content']=I('content');
		$data['addtime']=time();
		M('Service')->add($data);
		$this->ajaxReturn(array('status'=>1),"JSON");
	}
}